package vcp.client.GUI.mvc.view.addons;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.font.TextAttribute;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.utility.Common;
import vcp.client.utility.UtilityHelper;
import vcp.enums.Screen;
/**
 * 
 * @author VCP team
 *<p>
 *upper tool bar
 *</p>
 *@see UIAbstractView
 */
public class UpperToolBar extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JToolBar toolBar;
	private JLabel lblConnection;
	private JButton btnDemoScreen;
	private JLabel lblScreenName;
	private JPanel addtionalComponetsPanel;
	private JLabel lblCurrentTime;

	public UpperToolBar ()
	{
		super();
		this.resetView();
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * set tool bar connection display
	 * @param connectionStatus
	 */
	public void setToolBarConnectionDisplayLabelEnabled ( boolean connectionStatus )
	{
		if ( connectionStatus )
		{
			this.toggleConnectionLabel(Common.LBL_CONNECTED, Color.blue);
		}
		else
		{
			this.toggleConnectionLabel(Common.LBL_NOT_CONNECTED, Color.red);
		}
	}
	/**
	 * toggle demo option
	 * @param status
	 */
	public void toggleDemoOption ( boolean status )
	{
		this.btnDemoScreen.setEnabled(status);
	}
	/**
	 * add tool bar item
	 * @param items
	 */
	public void addToolBarItems ( ArrayList <JComponent> items )
	{
		for ( JComponent jComponent : items )
		{
			this.addtionalComponetsPanel.add(jComponent);
		}
	}
	/**
	 * remove tool bar items
	 * @param items
	 */
	public void removeToolBarItems ( ArrayList <JComponent> items )
	{
		for ( JComponent jComponent : items )
		{
			this.addtionalComponetsPanel.remove(jComponent);
		}
	}

	/**
	 * update the screen name by name
	 * @param name
	 */
	public void updateScreenName ( Screen name )
	{
		if ( name != null )
		{
			this.lblScreenName.setText(name.toString());
		}
	}
	/**
	 * update time on bar
	 * @param dateTime
	 */
	public void updateTime ( DateTime dateTime )
	{
		String time = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").print(dateTime);
		this.lblCurrentTime.setText(time);
	}
	/**
	 * reset clock
	 */
	public void resetClientClock ()
	{
		this.lblCurrentTime.setText("--/--/---- --:--");
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * toggle connection lable
	 * @param connectionLabel
	 * @param color
	 */
	private void toggleConnectionLabel ( String connectionLabel, Color color )
	{
		this.lblConnection.setText(connectionLabel);
		this.lblConnection.setForeground(color);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/

	@Override
	public void resetView ()
	{
		this.setToolBarConnectionDisplayLabelEnabled(false);
		this.toggleDemoOption(false);
	}

	@SuppressWarnings (
			{ "unchecked", "rawtypes" } )
	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(1024, 60));
		this.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		this.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 0));
		this.toolBar = new JToolBar();
		this.toolBar.setFloatable(false);
		this.toolBar.setRollover(true);
		this.toolBar.setPreferredSize(new Dimension(1000, 55));
		this.add(this.toolBar);

		this.btnDemoScreen = new JButton(Common.BTN_DEMO);
		this.btnDemoScreen.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnDemoScreen.setMaximumSize(new Dimension(75, 30));
		this.btnDemoScreen.setPreferredSize(new Dimension(75, 45));
		this.btnDemoScreen.setName(Common.BTN_DEMO);
		this.btnDemoScreen.setFocusable(false);
		this.toolBar.add(this.btnDemoScreen);

		Component horizontalStrut = Box.createHorizontalStrut(20);
		this.toolBar.add(horizontalStrut);

		this.lblConnection = new JLabel();
		this.lblConnection.setText("Not connected");
		Font lblConnectionFont = new Font("Lucida Grande", Font.PLAIN, 13);
		Map attributes = lblConnectionFont.getAttributes();
		attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
		this.lblConnection.setFont(lblConnectionFont.deriveFont(attributes));
		this.toolBar.add(this.lblConnection);

		JSeparator separator = new JSeparator();
		separator.setRequestFocusEnabled(false);
		separator.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		separator.setOrientation(SwingConstants.VERTICAL);
		this.toolBar.add(separator);

		Component horizontalStrut_4 = Box.createHorizontalStrut(20);
		this.toolBar.add(horizontalStrut_4);

		this.lblCurrentTime = new JLabel("--/--/---- --:--");
		this.toolBar.add(this.lblCurrentTime);

		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		this.toolBar.add(horizontalStrut_1);

		JLabel lblCurrentlyIn = new JLabel(Common.LBL_CURRENTLY_IN);
		this.toolBar.add(lblCurrentlyIn);

		this.lblScreenName = new JLabel();
		this.lblScreenName.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		this.toolBar.add(this.lblScreenName);

		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		this.toolBar.add(horizontalStrut_2);

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(650, 25));
		this.toolBar.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		this.addtionalComponetsPanel = new JPanel();
		panel.add(this.addtionalComponetsPanel, BorderLayout.CENTER);
		FlowLayout flowLayout_1 = (FlowLayout) this.addtionalComponetsPanel.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnDemoScreen.addActionListener(this);

	}

	@Override
	protected void setupButtonsIcons ()
	{
		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_GO_BACK, 24, 24);
		if ( icon != null )
		{
			this.btnDemoScreen.setIcon(icon);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( UpperToolBar.this.controllerDelegate != null )
		{
			UpperToolBar.this.controllerDelegate.onButtonPress(Screen.DEMO);
		}
	}

}
